package cn.itcast.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.itcast.myapplication.R;
import cn.itcast.myapplication.viewemodel.MyViewModel;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.add)
     Button add;

    @BindView(R.id.add2)
     Button add2;

    @BindView(R.id.tv_number)
     TextView tv_number;

     MyViewModel myViewModel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);//这段代码一定要加上不然会导致程序启动失败内存溢出，程序崩溃

//        myViewModel=new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(MyViewModel.class);
        myViewModel=new ViewModelProvider(this).get(MyViewModel.class);
//
//        add=(Button)findViewById(R.id.add);//加一
//
//        add2=(Button)findViewById(R.id.add2);//加二
//
//        tv_number=(TextView)findViewById(R.id.tv_number);//数字文本

        ButterKnife.bind(this);//这段代码一定要加上不然会导致程序启动失败内存溢出，程序崩溃

        tv_number.setText(String.valueOf(myViewModel.number));

        add2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                myViewModel.number+=2;

                tv_number.setText(String.valueOf(myViewModel.number));

            }

        });


        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                myViewModel.number++;
                tv_number.setText(String.valueOf(myViewModel.number));

            }

        });


    }
}

